# -*- coding: utf-8 -*-
import scrapy
from ..items import NewhouseItem
import time


class FangSpider(scrapy.Spider):
    name = 'fang'
    allowed_domains = ['sz.fang.ke.com']
    start_urls = ['https://sz.fang.ke.com']
    base_url = "https://sz.fang.ke.com/loupan/pg%d"

    def parse(self, response):
        for pg in range(1, 101):
            url = self.base_url % pg
            yield scrapy.Request(url=url, callback=self.parsePage)

    def parsePage(self, response):
        href_list = response.xpath('//div[@class="resblock-name"]/a/@href').extract()
        for href in href_list:
            url = self.start_urls[0] + href
            yield scrapy.Request(url=url, callback=self.parseDetail)

    def parseDetail(self, response):
        item = NewhouseItem()

        item['house_name'] = response.xpath('//div[2]/div[2]/div[1]/div[1]/h2/text()').extract_first()
        item['address'] = '/'.join(
            [site.rstrip('楼盘') for site in response.xpath('//div[@class="breadcrumbs"]/a/text()').extract()])
        item['site'] = response.xpath('//ul[@class="info-list"]/li[1]/span[2]/text()').extract_first()
        try:
            item['start_time'] = response.xpath(
                '//ul[@class="info-list"]/li[2]/div/span[2]/text()').extract_first().replace('.', '-')
        except:
            item['start_time'] = ''
        try:
            item['average_unit_price'] = response.xpath('//div[@class="price"]/span[2]/text()').extract()[0]
        except:
            item['average_unit_price'] = '价格待定'
        try:
            item['average_price'] = response.xpath('//div[@class="price"]/span[4]/text()').extract()[0] + '0000'
        except:
            item['average_price'] = '价格待定'

        try:
            item['house_type'] = '/'.join(response.xpath('//div[2]/div/div[3]/ul/li[3]/span/span/text()').extract())
            all_type = response.xpath(
                "//div[@class='mod-global']/div/div/div/div[2]/div/ul/li[1]/ul/div[@class='card-content']")
            for house in all_type:
                item['title'] = house.xpath('div[@class="content-title"]/text()').extract()[0].strip('\n').strip(' ')
                item['area'] = house.xpath('div[@class="content-area"]/text()').re('\d+')[0].strip('\n').strip(' ')
                item['price'] = house.xpath('div[@class="content-price"]/span/text()').extract()[0].strip('\n').strip(' ') + '0000'

                yield item
        except:
            item['house_type'] = ''
            item['title'] = ''
            item['area'] = ''
            item['price'] = ''
            yield item
